<?php
/**
 * 用户配方管理
 * 
 * @author  Lee<a605333742@gmail.com>
 * @time    2016-07-31
 */
namespace Admin\Controller;

class RecipeController extends CommonController{
    /*
     * 构造函数
     * 
     * @return #
     */
    public function _initialize() {
        parent::_initialize();
        $this->db       =new \Model\RecipeModel();
        $this->db_info  =new \Model\RecipecolorModel();
        $this->db_mes   =D("Comment");
    }
    
    /*
     * 用户配方列表
     * 
     * @return #
     */
    public function index(){
        if(I("request.name")){
            $map['name']    =I("request.name");
        }
        $map=$this->_search("Recipe");
        if(I("request.nickName")){
            $map['userId']  =A("Common")->getIds(I("request.nickName"),"nickName","User");
        }
        $map['state']   =2;
        if(session('type')<2){
            $map['userId'] =session("authId");
        }
        $sortBy     ='shareTime';
        $sort       =FALSE;
        $recipeList =$this->_list($this->db,$map,$sortBy,$sort);
        $this->assign("recipeList",$recipeList);
        $this->assign("act",1);
        $this->display();
    }
    
    /*
     * 待审核用户配方列表
     * 
     * @return #
     */
    public function auditRecipe(){
        if(I("request.name")){
            $map['name']    =I("request.name");
        }
        $map=$this->_search("Recipe");
        if(I("request.nickName")){
            $map['userId']  =A("Common")->getIds(I("request.nickName"),"nickName","User");
        }
        $map['state']   =1;
        if(session('type')<2){
            $map['userId'] =session("authId");
        }
        $sortBy     ='top';
        $sort       =desc;
        $recipeList =$this->_list($this->db,$map,$sortBy,$sort);
        $this->assign("recipeList",$recipeList);
        $this->assign("act",2);
        $this->display("index");
    }
    
    /*
     * 审核批量审核信息
     * 
     * @return bool
     */
    public function alledit(){       
        $map['id']      =I("request.id");
        $map['state']   =2;
        if($this->db->edits($map)){            
            $this->success("审核成功",U("index"));
        }else{
            $this->error("审核失败",U("index"));
        }
    }
    
    /*
     * 待审核，批量待审核
     * 
     * @return #
     */
    public function audit(){
        $map['id']      =I("request.id");
        $map['state']   =1;
        if($this->db->edits($map)){            
            $this->success("待审核成功",U("index"));
        }else{
            $this->error("待审核失败",U("index"));
        }
    }
    
    /*
     * 删除，批量删除求购信息
     * 
     * @return #
     */
    public function del(){
        $map['id']      =I("request.id");
        $map['state']   =3;
        if($this->db->edits($map)){            
            $this->success("删除成功",U("index"));
        }else{
            $this->error("删除失败",U("index"));
        }
    }
    
    /*
     * 配方详情表
     * 
     * @return #
     */
    public function recipeInfo(){
        $map['recipeId']=I("request.id",'',"int");
        $infoList   =$this->_list($this->db_info,$map);
        $this->assign("infoList",$infoList);
        $this->display();
    }
    
    /*
     * 删除、批量删除配方详情
     * 
     * @return #
     */
    public function delInfo(){
        $map['id']      =I("request.id");
        if($this->db->del($map)){            
            $this->success("删除成功",U("index"));
        }else{
            $this->error("删除失败",U("index"));
        }
    }
        
    /*
     * 留言列表
     * 
     * @return #
     */
    public function leavMessage(){
        $this->_search("LeavMessage");
        if(I("request.content")){
            $map['content'] =array("LIKE",'%'.trim(I("request.content")).'%');
        }
        if(I("request.add_time")){
            $map['commentTime'] =array("LIKE",'%'.trim(I("request.add_time")).'%');
        }
        $map['state']       =array("neq",3);
        $map['forumType']   =2;
        $list   =$this->_list($this->db_mes,$map);
        $this->assign("list",$list);
        $this->display();
    }
    
    /*
     * 显示新增留言
     * 
     * @return #
     */
    public function leavShow(){
        if(I("request.id")){
            $vo =$this->db_mes->find(I("request.id"));
            $this->assign("vo",$vo);
        }
        $this->display();
    }
    
    /*
     * 编辑留言
     * 
     * @return #
     */
    public function leaveEdit(){
        if(I("request.id")){
            //说明为修改
            if (false === $this->db_mes->create()) {
                $this->error($this->db_mes->getError());
            }
            if($this->db_mes->save()){
                $this->success("留言编辑成功");
            }else{
                $this->error("留言编辑失败");
            }
        }else{
            //为新新增
            if (false === $this->db_mes->create()) {
                $this->error($this->db_mes->getError());
            }
            if($this->db_mes->add()){
                $this->success("新增留言成功");
            }else{
                $this->error("新增留言失败");
            }
        }
    }
    
    /*
     * 删除留言
     * 
     * @return #
     */
    public function delLeavMes(){
        if(!I("request.id")){
            $this->error("参数错误");
        }
        if(is_array(I("request.id"))){
            $map['id']=array("in",implode(',',I("request.id")));            
        }else{
            $map['id']  =I("request.id");
        }
        $map['state']   =3;
        if($this->db_mes->save($map)){
            $this->success("删除成功");
        }else{
            $this->error("删除失败");
        }
    }
}